package com.wmlive.hhvideo.heihei.discovery.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.wmlive.hhvideo.common.base.DcBaseActivity;
import com.wmlive.hhvideo.heihei.beans.subject.TopicInfo;
import com.wmlive.hhvideo.heihei.discovery.adapter.SearchMusicViewPagerAdapter;
import com.wmlive.hhvideo.heihei.recordvideo.DyUIAPIManager;
import com.wmlive.hhvideo.heihei.recordvideo.UserShorVideoInfoExtraInfo;

import butterknife.BindView;
import cn.wmlive.hhvideo.R;

/**
 * Created by lsq on 6/2/2017.
 * 搜索音乐和视频的页面
 */

public class SearchMusicVideoActivity extends DcBaseActivity {

    @BindView(R.id.tabMusic)
    TabLayout tabMusic;
    @BindView(R.id.vpContainer)
    ViewPager vpContainer;

    private String startType;
    private TopicInfo topicInfo;

    public static void startSearchMusicActivity(Activity context, TopicInfo topicInfo) {
        Intent intent = new Intent(context, SearchMusicVideoActivity.class);
        Bundle bundle = new Bundle();
        bundle.putParcelable("topicInfo", topicInfo);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    /**
     * 从DyUIAPICallbackImpl类启动Activity
     *
     * @param context
     */
    public static void startfromDyUIAPI(Activity context, int requestCode) {
        Intent intent = new Intent(context, SearchMusicVideoActivity.class);
        intent.putExtra("startType", "fromDyUIAPI");
        context.startActivityForResult(intent, requestCode);
    }


    @Override
    protected void onSingleClick(View v) {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_search_music_video;
    }


    @Override
    protected void initData() {
        super.initData();
        topicInfo = getIntent().getParcelableExtra("topicInfo");
        startType = getIntent().getStringExtra("startType");
        tabMusic.setVisibility("fromDyUIAPI".equalsIgnoreCase(startType) ? View.GONE : View.VISIBLE);
        SearchMusicViewPagerAdapter viewPagerAdapter = new SearchMusicViewPagerAdapter(getSupportFragmentManager(), startType, topicInfo);
        vpContainer.setAdapter(viewPagerAdapter);
        setTabLayout();
    }

    private void setTabLayout() {
        tabMusic.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab != null) {
                    switch (tab.getPosition()) {
                        case 0:
                            vpContainer.setCurrentItem(0);
                            break;
                        case 1:
                            vpContainer.setCurrentItem(1);
                            break;
                        case 2:
                            UserShorVideoInfoExtraInfo userShorVideoInfoExtraInfo = null;
                            if (topicInfo != null) {
                                userShorVideoInfoExtraInfo = new UserShorVideoInfoExtraInfo();
                                userShorVideoInfoExtraInfo.setTopicInfo(topicInfo);
                            }
                            DyUIAPIManager.getInstance().onCameraByExtralInfo(SearchMusicVideoActivity.this, userShorVideoInfoExtraInfo, false);
                            getWeakHandler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            if (tabMusic != null && tabMusic.getTabAt(1) != null) {
                                                tabMusic.getTabAt(1).select();
                                            }
                                        }
                                    });
                                }
                            }, 1000);
                            break;
                    }
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        if (tabMusic != null && tabMusic.getTabAt(1) != null) {
            tabMusic.getTabAt(1).select();
        }
        vpContainer.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (tabMusic != null&&tabMusic.getTabAt(position) != null) {
                    tabMusic.getTabAt(position).select();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
